Private Sub DrawCurve(ByVal pic As PictureBox, ByVal start_t As Single, ByVal stop_t As Single, ByVal dt As Single)
Dim t As Single
pic.Cls
pic.CurrentX = X(start_t)
pic.CurrentY = Y(start_t)
t = start_t + dt
Do While t < stop_t
pic.Line -(X(t), Y(t))
t = t + dt
Loop
pic.Line -(X(stop_t), Y(stop_t))
End Sub
' Compute the Bezier curve parameters.
Private Sub GetBezierValues(ByVal ex1 As Single, ByVal ey1 As Single, ByVal ex2 As Single, ByVal ey2 As Single, ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single, ByRef Ax As Single, ByRef Bx As Single, ByRef Cx As Single, ByRef Dx As Single, ByRef Ay As Single, ByRef By As Single, ByRef Cy As Single, ByRef Dy As Single)
Ax = ex2 - ex1 - 3 * x2 + 3 * x1
Bx = 3 * ex1 - 6 * x1 + 3 * x2
Cx = -3 * ex1 + 3 * x1
Dx = ex1
Ay = ey2 - ey1 - 3 * y2 + 3 * y1
By = 3 * ey1 - 6 * y1 + 3 * y2
Cy = -3 * ey1 + 3 * y1
Dy = ey1
End Sub
' The parametric function Y(t).
Private Function Y(ByVal t As Single) As Single
Y = Ay * t ^ 3 + By * t * t + Cy * t + Dy
End Function
' The parametric function X(t).
Private Function X(ByVal t As Single) As Single
X = Ax * t ^ 3 + Bx * t * t + Cx * t + Dx
End Function
' Prepare to draw the Bezier curve.
Private Sub DrawBezier()
Dim dt As Single
Dim i As Integer
' Compute the curve parameters.
GetBezierValues _
PtX(1), PtY(1), _
PtX(4), PtY(4), _
PtX(2), PtY(2), _
PtX(3), PtY(3), _
Ax, Bx, Cx, Dx, Ay, By, Cy, Dy
' Draw the curve.
dt = CSng(txtDt.Text)
DrawCurve picCanvas, 0, 1, dt
If chkControlPoints.Value = vbChecked Then
' Draw the control points.
For i = 1 To NumPts
picCanvas.Line _
(PtX(i) - GAP, PtY(i) - GAP)- _
Step(2 * GAP, 2 * GAP), , BF
Next i
' Connect the control points.
picCanvas.DrawStyle = vbDot
picCanvas.CurrentX = PtX(1)
picCanvas.CurrentY = PtY(1)
For i = 2 To NumPts
picCanvas.Line -(PtX(i), PtY(i))
Next i
picCanvas.DrawStyle = vbSolid
End If
End Sub
' Select a point and start dragging it.
Private Sub picCanvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim i As Integer
' Find a close point.
For i = 1 To NumPts
If Abs(PtX(i) - X) <= GAP And _
Abs(PtY(i) - Y) <= GAP Then Exit For
Next i
If i > NumPts Then Exit Sub
Dragging = i
picCanvas.DrawMode = vbInvert
PtX(Dragging) = X
PtY(Dragging) = Y
picCanvas.Line _
(PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
Step(2 * GAP, 2 * GAP), , BF
End Sub
' Continue dragging a point.
Private Sub picCanvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Dragging < 1 Then Exit Sub
picCanvas.Line _
(PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
Step(2 * GAP, 2 * GAP), , BF
PtX(Dragging) = X
PtY(Dragging) = Y
picCanvas.Line _
(PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
Step(2 * GAP, 2 * GAP), , BF
End Sub
' Finish the drag and redraw the curve.
Private Sub picCanvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)